//
//  ViewController.swift
//  L14_03_UIView
//
//  Created by yao_yu on 14-10-6.
//  Copyright (c) 2014年 yao_yu. All rights reserved.
//

import UIKit

class ViewController: UIViewController {

    var label:UILabel?
    var toggle:UISwitch?
    
    override func viewDidLoad() {
        super.viewDidLoad()

        let center = view.center
        
        view.backgroundColor = UIColor.whiteColor()
        label = UILabel(frame: CGRectMake(0, 20, 300, 30))
        label?.text = "这就是我"
        label?.textAlignment = NSTextAlignment.Center
        label?.center = CGPointMake(center.x , center.y - 20)
        view.addSubview(label!)

        toggle = UISwitch(frame: CGRectMake(0, 20, 300, 30))
        toggle?.center = CGPointMake(center.x - 100, center.y + 20)
        toggle?.setOn(true, animated: true)
        toggle?.addTarget(self, action: "showHidden", forControlEvents: UIControlEvents.TouchUpInside)
        view.addSubview(toggle!)
        
        var button = UIButton.buttonWithType(UIButtonType.System) as UIButton
        button.frame = CGRectMake(0, 20, 60, 20)
        button.setTitle("背景色", forState: UIControlState.Normal)
        button.center = CGPointMake(center.x + 0, center.y + 20)
        view.addSubview(button)
        button.addTarget(self, action: "yySetColor", forControlEvents: UIControlEvents.TouchUpInside)
        
        button = UIButton.buttonWithType(UIButtonType.System) as UIButton
        button.frame = CGRectMake(0, 20, 60, 20)
        button.setTitle("改位置", forState: UIControlState.Normal)
        button.center = CGPointMake(center.x + 100, center.y + 20)
        view.addSubview(button)
        button.addTarget(self, action: "yySetPosition", forControlEvents: UIControlEvents.TouchUpInside)
    }
    
    func showHidden(){
        label?.hidden =  !toggle!.on
    }
    
    func yySetColor(){
        label?.backgroundColor = UIColor(red: 1.0, green: 0.5, blue: 0.5, alpha: 1.0)
    }
    
    func yySetPosition(){
        let center = (label!.center) as CGPoint
        if center.y > view.center.y - 50{
            label?.center = CGPointMake(center.x, center.y - 100)
        } else {
            label?.center = CGPointMake(center.x, center.y + 100)
        }
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }


}

